//
//  ViewController.m
//  网易新闻
//
//  Created by 吴志勇 on 2020/3/23.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "ViewController.h"
#import "TopLineViewController.h"
#import "HotViewController.h"
#import "ScoiertyViewController.h"
#import "ReaderViewController.h"
#import "VideoViewController.h"
#import "ScienceViewController.h"

#define KstatusH ([UIApplication sharedApplication].statusBarFrame.size.height)

@interface ViewController ()<UIScrollViewDelegate>


@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, weak) UIButton *selectBtn;

/*  */
@property (nonatomic, strong) NSMutableArray *titleBtnArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"瞎扯新闻";
//    添加标题滚动视图
    [self setupTitleScrollView];
    
//    添加内容滚动视图
    [self setupContentScrollView];
    
//    添加所有子控制器
    [self setupChildController];
    
//    添加所有标题按钮
    [self setupChildControllerTitle];
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

-(NSMutableArray *)titleBtnArray{
    if (_titleBtnArray == nil ) {
        _titleBtnArray = [NSMutableArray array];
    }
    return _titleBtnArray;
}

#pragma mark -------添加所有标题按钮
- (void)setupChildControllerTitle{
    NSInteger count = self.childViewControllers.count;
    CGFloat btnW = 100;
    CGFloat btnH = self.titleScrollView.bounds.size.height;
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.frame = CGRectMake(btnW * i , 0, btnW, btnH);
        [self.titleScrollView addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleBtnArray addObject:btn];
        if (i == 0) {
            [self btnClick:btn];
        }
    }
    self.titleScrollView.contentSize = CGSizeMake(btnW *count, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width *count, 0);
}

//改变按钮颜色
- (void)selButton:(UIButton *)btton{
    _selectBtn.transform = CGAffineTransformIdentity;
    [_selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
   
    [self setUpTitleCenter:btton];
    [self setupTitleScale:btton];
    
     _selectBtn  =  btton;
}

#pragma mark -------字体缩放
- (void)setupTitleScale:(UIButton *)btn{
    btn.transform = CGAffineTransformMakeScale(1.3,1.3);
}


#pragma mark -------标题居中

- (void)setUpTitleCenter:(UIButton *)btn{
    CGFloat offset = btn.center.x - [[UIScreen mainScreen]bounds].size.width*0.5;
    if (offset < 0 ) {
        offset = 0;
    }
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width - [[UIScreen mainScreen]bounds].size.width;
    
    if (offset > maxOffsetX) {
        offset = maxOffsetX;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
}

#pragma mark -------处理标题点击
- (void)btnClick:(UIButton *)button{
    [self selButton:button];
    
    CGFloat x = button.tag * [UIScreen mainScreen].bounds.size.width;
    [self setOneViewControlleView:button.tag distancex:x];
    self.contentScrollView.contentOffset  = CGPointMake(x, 0);
    
}

#pragma mark -------添加所有子控制器
- (void)setupChildController{
    TopLineViewController *vc1 = [[TopLineViewController alloc] init];
    vc1.title = @"头条";
    [self addChildViewController:vc1];
    
    HotViewController *vc2 = [[HotViewController alloc] init];
    vc2.title = @"热点";
    [self addChildViewController:vc2];
    
    ScoiertyViewController *vc3 = [[ScoiertyViewController alloc] init];
    vc3.title = @"社会";
    [self addChildViewController:vc3];
    
    ReaderViewController *vc4 = [[ReaderViewController alloc] init];
    vc4.title = @"订阅";
    [self addChildViewController:vc4];
    
    VideoViewController *vc5 = [[VideoViewController alloc] init];
    vc5.title = @"视频";
    [self addChildViewController:vc5];
    
    ScienceViewController *vc6 = [[ScienceViewController alloc] init];
    vc6.title = @"科学";
    [self addChildViewController:vc6];
}


- (void)setOneViewControlleView:(NSInteger)index distancex:(CGFloat)x{
    UIViewController *vc = self.childViewControllers[index];
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
}

#pragma mark -------添加标题滚动视图
- (void)setupTitleScrollView{
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = self.navigationController.navigationBarHidden?KstatusH:self.navigationController.navigationBar.bounds.size.height+KstatusH;
    titleScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, 44);
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
}

#pragma mark -------添加内容滚动视图
- (void)setupContentScrollView{
    
    UIScrollView *contentView = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(_titleScrollView.frame);
    contentView.frame = CGRectMake(0,y, self.view.bounds.size.width, self.view.bounds.size.height - y);
    contentView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:contentView];
    
    self.contentScrollView = contentView;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
}

#pragma mark -------UIScrollviewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger i =  scrollView.contentOffset.x/ [[UIScreen mainScreen] bounds].size.width;
    UIButton *btn = self.titleBtnArray[i];
    [self selButton:btn];
    
    [self setOneViewControlleView:i distancex:(i *[[UIScreen mainScreen] bounds].size.width)];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger i =  scrollView.contentOffset.x/ [[UIScreen mainScreen] bounds].size.width;
    NSLog(@"%ld",i);
    UIButton *leftBtn = self.titleBtnArray[i];
    NSInteger count = self.titleBtnArray.count;
    UIButton *rightBtn;
    if (i+1 < count) {
        rightBtn = self.titleBtnArray[i+1];
    }
    
    CGFloat scaleR = scrollView.contentOffset.x/ [[UIScreen mainScreen] bounds].size.width;
    scaleR -= i;
    CGFloat scaleL = 1 - scaleR;
    leftBtn.transform = CGAffineTransformMakeScale(scaleL*0.3 +1, scaleL *0.3 +1);
    rightBtn.transform = CGAffineTransformMakeScale(scaleR*0.3 +1, scaleR *0.3 +1);
    
}


@end
